package com.leetcode.LC;

import java.util.Arrays;

public class LC1046 {
    public int lastStoneWeight(int[] stones) {
        //不断对石头进行粉碎
        int size = stones.length;
        int[] a = stones;
        while (size > 1) {
            int[] b = toArrayStone(a);
            size = b.length;
            a = b;
        }
        if (size == 0) {
            return 0;
        } else {
            return a[0];
        }
    }

    public int[] toArrayStone(int[] stones) {
        Arrays.sort(stones);
        if (stones[stones.length - 1] == stones[stones.length - 2]) {//最后两块石头一样
            int[] ints = new int[stones.length - 2];
            for (int i = 0; i < ints.length; i++) {
                ints[i] = stones[i];
            }
            return ints;
        } else {//最后两块石头不一样
            int[] ints = new int[stones.length - 1];
            for (int i = 0; i < ints.length; i++) {
                ints[i] = stones[i];
            }
            ints[ints.length - 1] = stones[stones.length - 1] - stones[stones.length - 2];
            Arrays.sort(ints);
            return ints;
        }
    }

    public static void main(String[] args) {
        System.out.println(new LC1046().lastStoneWeight(new int[]{2, 7, 4, 1, 8, 1}));
    }
}
